Page({
  data: {
    currentStatus: '',
    orderList: [
      {
        id: 1,
        status: 'created',
        statusText: '待支付',
        serviceImage: '/assets/images/service1.jpg',
        serviceName: '全身放松按摩',
        technicianName: '张技师',
        appointmentTime: '2023-05-20 14:00',
        price: 198,
        actions: [
          { action: 'pay', text: '立即支付' },
          { action: 'cancel', text: '取消订单' }
        ]
      },
      {
        id: 2,
        status: 'paid',
        statusText: '待服务',
        serviceImage: '/assets/images/service2.jpg',
        serviceName: '足底按摩',
        technicianName: '李技师',
        appointmentTime: '2023-05-18 10:00',
        price: 128,
        actions: [
          { action: 'contact', text: '联系技师' }
        ]
      }
    ],
    page: 1,
    pageSize: 10,
    noMoreData: false
  },

  onLoad: function() {
    this.loadOrderList();
  },

  loadOrderList: function() {
    const app = getApp();
    const { currentStatus, page, pageSize } = this.data;
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/`,
      data: {
        status: currentStatus,
        page: page,
        page_size: pageSize
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const newList = res.data.results || res.data;
          const orderList = page === 1 ? newList : [...this.data.orderList, ...newList];
          
          this.setData({
            orderList: orderList,
            noMoreData: newList.length < pageSize
          });
        }
      }
    });
  },

  switchStatus: function(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({
      currentStatus: status,
      page: 1
    });
    this.loadOrderList();
  },

  goToOrderDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/customer/orderDetail?id=${id}`
    });
  },

  handleAction: function(e) {
    const action = e.currentTarget.dataset.action;
    const orderId = e.currentTarget.dataset.id;
    
    switch(action) {
      case 'pay':
        this.payOrder(orderId);
        break;
      case 'cancel':
        this.cancelOrder(orderId);
        break;
      case 'contact':
        this.contactTechnician(orderId);
        break;
    }
  },

  payOrder: function(orderId) {
    // 跳转到支付页面
    wx.navigateTo({
      url: `/pages/customer/payment?orderId=${orderId}`
    });
  },

  cancelOrder: function(orderId) {
    const app = getApp();
    
    wx.showModal({
      title: '确认取消',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          app.request({
            url: `${app.globalData.baseUrl}/orders/${orderId}/cancel/`,
            method: 'POST',
            success: (res) => {
              if (res.statusCode === 200) {
                wx.showToast({
                  title: '取消成功',
                  icon: 'success'
                });
                // 重新加载订单列表
                this.loadOrderList();
              } else {
                wx.showToast({
                  title: '取消失败',
                  icon: 'none'
                });
              }
            }
          });
        }
      }
    });
  },

  contactTechnician: function(orderId) {
    // 联系技师功能
    wx.showToast({
      title: '联系技师功能',
      icon: 'none'
    });
  },

  onReachBottom: function() {
    // 上拉加载更多
    if (!this.data.noMoreData) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadOrderList();
    }
  }
})